package com.portalsys.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.portalsys.model.MessageContentModel;
import com.portalsys.model.PlateTypeModel;
import com.portalsys.service.MessageContentService;
import com.sys.ms.action.BaseAction;
import com.sys.utils.HtmlUtil;
import com.sys.utils.UUID;

@Controller
@RequestMapping("/messageContent") 
public class MessageContentController extends BaseAction {

private final static Logger log= Logger.getLogger(MessageContentController.class);
	
	static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static void main(String[] args) {
		System.out.println(sdf.format(new Date()));
	}
	@Autowired
	MessageContentService<MessageContentModel> messageContentService;
	
	/**
	 * 留言板列表
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/messageContentMain") 
	public ModelAndView newInfoMain(PlateTypeModel model,HttpServletRequest request) {
		Map<String,Object>  context = getRootMap();
		return forword("portal/messageContentMain",context); 
	}
	
	/**
	 * json 列表页面
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/dataList") 
	public void dataList(MessageContentModel model,HttpServletRequest request, HttpServletResponse response) {
		try {
			List<MessageContentModel> dataList = messageContentService.queryByList(model);
			//设置页面数据
			Map<String,Object> jsonMap = new HashMap<String,Object>();
			jsonMap.put("total",model.getPager().getRowCount());
			jsonMap.put("rows", dataList);
			HtmlUtil.writerJson(response, jsonMap);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
	}
	@RequestMapping("/getId")
	public void getId(String id,HttpServletResponse response,HttpServletRequest request) throws Exception{
		Map<String,Object>  context = new HashMap<String,Object>();
		MessageContentModel bean  = messageContentService.queryById(id);
		if(bean  == null){
			sendFailureMessage(response, "没有找到对应的记录!");
			return;
		}
		context.put(SUCCESS, true);
		context.put("data", bean);
		HtmlUtil.writerJson(response, context);
	}
	@RequestMapping("/save")
	public void save(MessageContentModel bean ,HttpServletResponse response) throws Exception{
		if(bean.getId() == null || "".equals(bean.getId())){
			bean.setId(UUID.getUUIDKey());
			bean.setDate(sdf.format(new Date()));
			messageContentService.add(bean);
		}else{
			messageContentService.update(bean);
		}
		sendSuccessMessage(response, "保存成功~");
	}
	
	@RequestMapping("/delete")
	public void delete(String[] id,HttpServletResponse response) throws Exception{
		messageContentService.delete(id);
		sendSuccessMessage(response, "删除成功");
	}
}
